//
//  EView.swift
//  App_Login
//
//  Created by Flutter on 2021/7/15.
//

import UIKit
import UICore
import Leaf
import SnapKit

class EView: View {
    
    enum Direction: Int {
        case Right      = 0
        case Down       = 1
        case Left       = 2
        case Up         = 3
        case Unknown    = 4
    }
    
    private(set) var direction: Direction = .Right
    private(set) var eSize: CGSize = CGSize.zero
    
    private lazy var contentView: View = {
        View()
            .leaf
            .backgroundColor(.white)
            .instance
    }()
    
    private lazy var view1: View = {
        View()
            .leaf
            .backgroundColor(.black)
            .instance
    }()
    
    private lazy var view2: View = {
        View()
            .leaf
            .backgroundColor(.black)
            .instance
    }()
    
    private lazy var view3: View = {
        View()
            .leaf
            .backgroundColor(.black)
            .instance
    }()
    
    private lazy var view4: View = {
        View()
            .leaf
            .backgroundColor(.black)
            .instance
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}

// MARK: - Method

private extension EView {
    func setupUI(){
        
        contentView.leaf.add(to: self).snp.makeConstraints {
            $0.center.equalToSuperview()
            $0.size.equalTo(CGSize.zero)
        }
        view1.leaf.add(to: contentView).snp.makeConstraints {
            $0.trailing.top.leading.equalToSuperview()
            $0.height.equalToSuperview().multipliedBy(0.2)
        }
        
        view2.leaf.add(to: contentView).snp.makeConstraints {
            $0.trailing.centerY.leading.equalToSuperview()
            $0.height.equalToSuperview().multipliedBy(0.2)
        }
        view3.leaf.add(to: contentView).snp.makeConstraints {
            $0.trailing.bottom.leading.equalToSuperview()
            $0.height.equalToSuperview().multipliedBy(0.2)
        }
        view4.leaf.add(to: contentView).snp.makeConstraints {
            $0.bottom.top.leading.equalToSuperview()
            $0.width.equalToSuperview().multipliedBy(0.2)
        }
        
//        rotateRandomly()
        
    }
}


// MARK: - Public Method

extension EView {
    
    @discardableResult
    func rotateRandomly() -> Direction {
        
        let directionNumber = Int(arc4random_uniform(4))
        direction = Direction(rawValue: directionNumber) ?? .Right
        let rotationAngle = CGFloat(direction.rawValue) * (CGFloat.pi/2)
        contentView.transform = CGAffineTransform(rotationAngle: rotationAngle)
        return direction
    }
    
    func rotate(direction: Direction) {
        if self.direction != direction {
            self.direction = direction
            let rotationAngle = CGFloat(direction.rawValue) * (CGFloat.pi/2)
            contentView.transform = CGAffineTransform(rotationAngle: rotationAngle)
        }
    }
    
    func resize(size: CGSize) {
        if self.eSize != size {
            self.eSize = size
            contentView.snp.updateConstraints {
                $0.size.equalTo(size)
            }
        }
    }
    
    

}
